package com.ruoyi.fileMaintenance.xfyunApi;

import cn.xfyun.api.TtsClient;
import cn.xfyun.model.response.TtsResponse;
import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import okhttp3.Response;
import okhttp3.WebSocket;

import java.io.File;


/**
 * /**
 * TTS ( Text to Speech ):语音合成
 *
 * @author yingpeng
 */
public class TtsClientApp {

    private static final String appId = "591609d9";
    private static final String apiSecret = "ZGUwYjg1Y2MxZWE4YTgyNzFjNTk5MGU3";
    private static final String apiKey = "1b0bccef2be723734b7754152b767c00";
    private static String message;

    public static String LanguageSynthesis(String fileName) {
        // 上传文件路径
        String filePath = RuoYiConfig.getVoiceFiles();
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(filePath + "/" + fileName + ".mp3");

            TtsClient ttsClient = new TtsClient.Builder()
                    .signature(appId, apiKey, apiSecret)
                    .build();

            ttsClient.send(fileName, new AbstractTtsWebSocketListener(file) {
                @Override
                public void onSuccess(byte[] bytes) {
                }

                @Override
                public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
                    System.out.println(throwable.getMessage());
                }

                @Override
                public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
                    System.out.println(ttsResponse.toString());
                }
            });
        } catch (Exception e) {
            System.out.println(message);
            System.out.println("错误码查询链接：https://www.xfyun.cn/document/error-code");
        }
        return "/voiceFiles/" + fileName + ".mp3";
    }
}
